/*
 * CommandLineParser.c
 *
 *  Created on: 23/03/2011
 *      Author: luis
 */

#include "CommandLineParser.h"
#include <string.h>
#include <stdio.h>

void command_line_parser_create(CommandLineParser * parser, int size, char ** argv){
	parser->argv = argv;
	parser->executor = (Executor*)malloc(sizeof(Executor));
	executor_create(parser->executor);
	parser->sizeOfParams = size;
	parser->paramCounter = 1;
	parser->mode = UNDEFINED;
}

void command_line_parser_parse_string(CommandLineParser * parser, char ** argument){
	char * returnString = parser->argv[parser->paramCounter];
	if (returnString[0] != '-'){
		*argument = returnString;
	}
}


void command_line_parser_parse_translate(CommandLineParser * parser){
	char * arg1 = NULL;
	command_line_parser_parse_string(parser, &arg1);
	if (arg1 != NULL){
		parser->paramCounter++;
		char * arg2 = NULL;
		command_line_parser_parse_string(parser, &arg2);
		if (arg2 != NULL){
			executor_change_first_mode(parser->executor, TRANSLATE);
			executor_set_first_argument(parser->executor, arg1);
			executor_set_second_argument(parser->executor, arg2);
			parser->paramCounter++;
		}
		else {
			parser->errorMsj = "Faltan parametros";
		}
	}
	else {
		parser->errorMsj = "Error en entrada de parametros";
	}
}

void command_line_parser_parse_squeeze(CommandLineParser * parser){
	parser->paramCounter++;
	char * arg1 = NULL;
	command_line_parser_parse_string(parser, &arg1);
	if (arg1 != NULL){
		parser->paramCounter++;
		if (parser->paramCounter < parser->sizeOfParams){
			char * arg2 = NULL;
			command_line_parser_parse_string(parser, &arg2);
			if (arg2 != NULL){
				executor_change_first_mode(parser->executor, TRANSLATE);
				executor_change_second_mode(parser->executor, SUMMARIZE);
				executor_set_first_argument(parser->executor, arg1);
				executor_set_second_argument(parser->executor, arg2);
				parser->paramCounter++;
			}
		}
		else {
			executor_change_first_mode(parser->executor, SUMMARIZE);
			executor_set_first_argument(parser->executor, arg1);
		}
	}
	else {
		parser->errorMsj = "Parametro invalido";
	}
}

void command_line_parser_parse_delete(CommandLineParser * parser){
	parser->paramCounter++;
	char * arg1 = NULL;
	command_line_parser_parse_string(parser, &arg1);
	if (arg1 != NULL){
		parser->paramCounter++;
		executor_change_first_mode(parser->executor, DELETE);
		executor_set_first_argument(parser->executor, arg1);
	}
	else {
		parser->errorMsj = "Parametro invalido";
	}
}

void command_line_parser_show_version(CommandLineParser * parser){
	printf ("Version 1.0\n");
	parser->mode = VERSION;
}

void command_line_parser_show_help(CommandLineParser * parser){
	printf ("Usage: \n"
			"\ttp0 -h\n"
			"\ttp0 -V\n"
			"\ttp0 [options] string1 string2\n"
			"\ttp0 [options] string1\n");
	printf ("Options:\n"
			"\t-V, --version \tPrint version and quit.\n"
			"\t-h, --help \tPrint this information and quit.\n"
			"\t-d, --delete \tDelete characters in string1\n"
			"\t-s, --squeeze \tSqueeze characters in input.\n");
	parser->mode = HELP;
}

void command_line_parser_parse_both(CommandLineParser * parser){
	parser->paramCounter++;
	char * arg1 = NULL;
	command_line_parser_parse_string(parser, &arg1);
	if (arg1 != NULL){
		parser->paramCounter++;
		char * arg2 = NULL;
		command_line_parser_parse_string(parser, &arg2);
		if (arg2 != NULL){
			executor_change_first_mode(parser->executor, DELETE);
			executor_change_second_mode(parser->executor, SUMMARIZE);
			executor_set_first_argument(parser->executor, arg1);
			executor_set_second_argument(parser->executor, arg2);
			parser->paramCounter++;
		}
		else {
			parser->errorMsj = "Hay mas argumentos";
		}
	}
	else {
		parser->errorMsj = "Parametro invalido";
	}
}

void command_line_parser_parse_parameters(CommandLineParser * parser){
	while (parser->paramCounter < parser->sizeOfParams && parser->errorMsj == NULL && parser->mode != HELP && parser->mode != VERSION){
		if ((strcmp(parser->argv[parser->paramCounter], "-s")==0)||(strcmp(parser->argv[parser->paramCounter], "--squeeze")==0)){
			command_line_parser_parse_squeeze(parser);
		}
		else if ((strcmp(parser->argv[parser->paramCounter], "-d")==0)||(strcmp(parser->argv[parser->paramCounter], "--delete")==0)){
			command_line_parser_parse_delete(parser);
		}
		else if ((strcmp(parser->argv[parser->paramCounter], "-V")==0)||(strcmp(parser->argv[parser->paramCounter], "--version")==0)){
			command_line_parser_show_version(parser);
		}
		else if ((strcmp(parser->argv[parser->paramCounter], "-h")==0)||(strcmp(parser->argv[parser->paramCounter], "--help")==0)){
			command_line_parser_show_help(parser);
		}
		else if ((strcmp(parser->argv[parser->paramCounter], "-sd")==0)||(strcmp(parser->argv[parser->paramCounter], "-ds")==0)){
			command_line_parser_parse_both(parser);
		}
		else if (parser->paramCounter == 1){
			command_line_parser_parse_translate(parser);
		}
		else {
			parser->errorMsj = "Parametro incorrecto";
		}
	}
}

Executor * command_line_parser_get_executor(CommandLineParser * parser){
	if (executor_get_mode(parser->executor) == UNDEFINED || parser->mode == HELP || parser->mode == VERSION){
		return NULL;
	}
	return parser->executor;
}

char * command_line_get_error(CommandLineParser * parser){
	return parser->errorMsj;
}

void command_line_parser_delete(CommandLineParser * parser){
	executor_delete(parser->executor);
	free(parser);
}


